<?php
namespace Home\Controller;
use Common\Controller\BaseController;
use Think\Controller;
class LoginController extends BaseController {

    public function index() {
        if(!is_login()){
            $this->display('login');
        }else{
            $this->redirect('/');
        }
    }

    public function login_do(){
        if(!IS_POST){
            $this->error('非法访问');
        }
        $access_type = I('post.access');
        if(empty($access_type)){
            $this->ajaxReturn(['message'=>'错误的提交方式','code'=>101]);
        }
        if($access_type != 'xhr'){
            $this->ajaxReturn(['message'=>'不正确提交','code'=>102]);
        }
        $mobile = trim(I('post.mobile'));
        $password = trim(I('post.password'));
        if(empty($mobile) || !is_mobile($mobile)){
            $this->ajaxReturn(['message'=>'手机号码格式不正确','code'=>103]);
        }
        if(empty($password) || !is_password($password)){
            $this->ajaxReturn(['message'=> '密码为空或格式不正确','code'=>104]);
        }
        //检查完毕，开始请求登陆接口
        $post['mobile'] = $mobile;
        $post['password'] = $password;
        $post['login_type'] = 'normal';
        $login_res = json_decode($this->http->curl_request(BASE_URL.'web_login',$post),true);
        if(isset($login_res['code'])){
            if($login_res['code'] == 200) {
                //登陆成功，则将用户登陆信息写入session中
                $_SESSION['user_login'] = $login_res['results'];
                $this->ajaxReturn(['code'=>200,'message'=>'登陆成功']);
            }else{
                $this->ajaxReturn(['code'=>$login_res['code'],'message'=>$login_res['message']]);
            }
        }else{
            $this->ajaxReturn(['code'=>105,'message'=>'请求失败']);
        }
    }

    public function sign(){

        if(is_login()){
            $this->redirect('/owner');
        }
        if(!IS_GET){
            $this->error('非法访问');
        }
        $this->display('sign');

    }

    public function send_sms() {
        if(!IS_POST){
            $this->error('非法请求');
        }
        $access_type = I('post.access');
        if(empty($access_type)){
            $this->ajaxReturn(['message'=>'错误的提交方式','code'=>101]);
        }
        if($access_type != 'xhr'){
            $this->ajaxReturn(['message'=>'不正确提交','code'=>102]);
        }
        $mobile = trim(I('post.mobile'));
        if(empty($mobile) || !is_mobile($mobile)){
            $this->ajaxReturn(['message'=>'手机号码格式不正确','code'=>103]);
        }
        $code_type = trim(I('post.type'));
        if(empty($code_type) || in_array($code_type,['sign,change_password'])){
            $this->ajaxReturn(['code'=>104,'message'=>'短信请求类别错误']);
        }
        $post['mobile'] = $mobile;
        $post['code_type'] = $code_type;
        //请求发送手机短信
        $sms_send_res = json_decode($this->http->curl_request(BASE_URL.'smssend',$post),true);
        if(isset($sms_send_res['code'])){
            if($sms_send_res['code'] == 200){
                $this->ajaxReturn(['code'=> 200,'message'=>'发送成功']);
            }else{
                $this->ajaxReturn(['code'=>$sms_send_res['code'],'message'=>$sms_send_res['message']]);
            }
        }else{
            $this->ajaxReturn(['code'=>105,'message'=>'网络错误']);
        }
    }


    public function sign_do(){
        if(!IS_POST){
            $this->error('非法请求');
        }
        $access_type = I('post.access');
        if(empty($access_type)){
            $this->ajaxReturn(['message'=>'错误的提交方式','code'=>101]);
        }
        if($access_type != 'xhr'){
            $this->ajaxReturn(['message'=>'不正确提交','code'=>102]);
        }
        $mobile = trim(I('post.mobile'));
        $code = trim(I('post.code'));
        if(empty($mobile) || !is_mobile($mobile)){
            $this->ajaxReturn(['message'=>'手机号码格式不正确','code'=>103]);
        }
        if(empty($code) || !is_smsCode($code)){
            $this->ajaxReturn(['code'=>106,'message'=>'非法的验证码']);
        }
        $code_type = trim(I('post.type'));
        if(empty($code_type) || in_array($code_type,['sign,change_password'])){
            $this->ajaxReturn(['code'=>104,'message'=>'短信请求类别错误']);
        }
        $password = trim(I('post.password'));
        if(empty($password) || !is_password($password)){
            $this->ajaxReturn(['message'=> '密码为空或格式不正确','code'=>104]);
        }
        //先请求短信验证码验证接口
        $post['code'] = $code;
        $post['mobile'] = $mobile;
        $post['code_type'] = $code_type;
        $smsCode_check = json_decode($this->http->curl_request(BASE_URL.'code_check',$post),true);
        if(isset($smsCode_check['code'])){
            if($smsCode_check['code'] == 200){
                //验证短信验证码成功，现在开始注册
                $this->doSign($mobile,$password);
            }else{
                $this->ajaxReturn(['code'=>$smsCode_check['code'],'message'=>$smsCode_check['message']]);
            }
        }else{
            $this->ajaxReturn(['code'=>105,'message'=>'网络错误']);
        }
    }


    protected function doSign($mobile,$password) {
        $post['mobile'] = $mobile;
        $post['password'] = $password;
        $post['sign_way'] = 'mWeb';
        $sign_res = json_decode($this->http->curl_request(BASE_URL.'user',$post),true);
        if(isset($sign_res['code'])){
            if($sign_res['code'] == 200){
                //验证短信验证码成功，现在开始注册
                $_SESSION['user_login'] = $sign_res['results'];
                $this->ajaxReturn(['code'=> 200,'message'=>'发送成功']);
            }else{
                $this->ajaxReturn(['code'=>$sign_res['code'],'message'=>$sign_res['message']]);
            }
        }else{
            $this->ajaxReturn(['code'=>105,'message'=>'网络错误']);
        }
    }

    function loginOut() {
        if(!IS_GET) {
            $this->error('非法访问');
        }
        unset($_SESSION['user_login']);
        if(!is_login()){
            $this->redirect('/');
        }else{
            $this->error('退出失败，请稍后再试');
        }
    }

    function forget_password() {
        $this->display('Public/download');
    }


}